clear
set matsize 5000
cd <directory>
set matsize 5000


**********************************************************************************************************************
**Table 4- Columns 1-2 (Productivity-Temperature Gradient)
**********************************************************************************************************************

use "LEDprojecwithtamudatanew.dta" , clear

*drop post-LED observations
keep if led==0

*Generate factory by year, factory by month, day of the week FE
xi i.unit*i.year  i.unit*i.month i.dow

*Spline Variables for Temperature
mkspline wbgtmeant1 19 wbgtmeant2=wbgtmeant, displayknots

foreach i of numlist 1/7{

mkspline wbgtmeantlag`i'1 19 wbgtmeantlag`i'2=wbgtmeantlag`i', displayknots
}



global depvars mactualeff


*residualize temperature and rainfall - partial out budgeted efficiency, production line FE, factory by year, factory by month, day of the week FE
foreach i of varlist wbgtmeant1 wbgtmeant2 wbgtmeantlag* precipitation{

areg `i' mbudgeteff _I*,  absorb(rno) cluster(unit)
predict `i'res, res

}

*residualize productivity- partial out budgeted efficiency, production line FE, factory by year, factory by month, day of the week FE

foreach i of varlist $depvars{

areg `i' mbudgeteff  _I*,  absorb(rno) cluster(unit)
predict `i'res, res

}


*Program for getting confidence intervals from output of boottest
cap program drop getci
program define getci
	matrix ci=r(CI)
	scalar lci=ci[1,1]
	scalar uci=ci[1,2]
	
	end
	

foreach i of varlist $depvars{
foreach var of varlist wbgtmeant {

cap erase "eff_ledinteraction_yearbymonth_noled_7dayDL_`i'.xml"
cap erase "eff_ledinteraction_yearbymonth_noled_7dayDL_`i'.txt"

cap erase "eff_ledinteraction_yearbymonth_noled_7dayDL_log`i'.xml"
cap erase "eff_ledinteraction_yearbymonth_noled_7dayDL_log`i'.txt"
	
}
}

foreach i of varlist $depvars{

foreach var of varlist wbgtmeant {

	reg `i'res precipitationres `var'1res `var'2res `var'lag11res `var'lag12res `var'lag21res `var'lag22res `var'lag31res `var'lag32res `var'lag41res `var'lag42res `var'lag51res `var'lag52res `var'lag61res `var'lag62res `var'lag71res `var'lag72res,  cluster(unit)
			
	su `i' if e(sample)
	local mean=r(mean)
	
	boottest  `var'1res, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	
	outreg2 using "eff_ledinteraction_yearbymonth_noled_7dayDL_`i'", stats(coef) noaster ctitle("rain control, LED") keep (`var'1res) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci),mean of dep var, `mean')

	boottest  `var'2res, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "eff_ledinteraction_yearbymonth_noled_7dayDL_`i'", stats(coef) noaster ctitle("rain control, LED")  keep (`var'2res) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))
	
	
	boottest precipitationres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "eff_ledinteraction_yearbymonth_noled_7dayDL_`i'", stats(coef) noaster ctitle("rain control, LED")  keep (precipitationres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))
	
}
}

**********************************************************************************************************************
**Table 4 (Columns 3 and 4) - Interactions with the LED adoption dummy
**********************************************************************************************************************

use "LEDprojecwithtamudatanew.dta" , clear

*Generate factory by year, factory by month, day of the week FE
xi i.unit*i.year  i.unit*i.month i.dow

*Spline Variables for Temperature
mkspline wbgtmeant1 19 wbgtmeant2=wbgtmeant, displayknots

foreach i of numlist 1/7{
mkspline wbgtmeantlag`i'1 19 wbgtmeantlag`i'2=wbgtmeantlag`i', displayknots

}

foreach var of varlist wbgtmeant {
foreach i of numlist 1/2{
g `var'`i'led=`var'`i'*led
replace `var'`i'led=. if `var'==. | led==.

}
}


foreach var of varlist wbgtmeant{
foreach i of numlist 1/2{
foreach j of numlist 1/7{
g `var'lag`j'`i'led=`var'lag`j'`i'*led
}
}
}

global depvars mactualeff

*residualize temperature and rainfall - partial out budgeted efficiency, production line FE, factory by year, factory by month, day of the week FE

foreach i of varlist wbgtmeant1 wbgtmeant2 wbgtmeantlag* wbgtmeant1led wbgtmeant2led precipitation led{

areg `i' mbudgeteff _I*,  absorb(rno) cluster(unit)
predict `i'res, res


}

*residualize productivity- - partial out budgeted efficiency, production line FE, factory by year, factory by month, day of the week FE
foreach i of varlist $depvars{

areg `i' mbudgeteff  _I*,  absorb(rno) cluster(unit)
predict `i'res, res

}


*Program for getting confidence intervals from output of boottest
cap program drop getci
program define getci
	matrix ci=r(CI)
	scalar lci=ci[1,1]
	scalar uci=ci[1,2]
	
	end
	
foreach i of varlist $depvars{
foreach var of varlist wbgtmeant {

cap erase "eff_ledinteraction_yearbymonth_7dayDL_`i'.xml"
cap erase "eff_ledinteraction_yearbymonth_7dayDL_`i'.txt"

cap erase "eff_ledinteraction_yearbymonth_7dayDL_log`i'.xml"
cap erase "eff_ledinteraction_yearbymonth_7dayDL_log`i'.txt"
	
}
}

foreach i of varlist $depvars{

foreach var of varlist wbgtmeant {

	reg `i'res precipitationres `var'1ledres `var'2ledres `var'lag11ledres `var'lag12ledres `var'lag21ledres `var'lag22ledres  `var'lag31ledres `var'lag32ledres `var'lag41ledres `var'lag42ledres `var'lag51ledres `var'lag52ledres `var'lag61ledres `var'lag62ledres `var'lag71ledres `var'lag72ledres  `var'1res `var'2res `var'lag11res `var'lag12res `var'lag21res `var'lag22res `var'lag31res `var'lag32res `var'lag41res `var'lag42res `var'lag51res `var'lag52res `var'lag61res `var'lag62res `var'lag71res `var'lag72res ledres,  cluster(unit)
			
	su `i' if e(sample)
	local mean=r(mean)
	
	boottest  `var'1res, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	
	outreg2 using "eff_ledinteraction_yearbymonth_7dayDL_`i'", stats(coef) noaster ctitle("rain control, LED") keep (`var'1res) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci),mean of dep var, `mean')

	boottest  `var'2res, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "eff_ledinteraction_yearbymonth_7dayDL_`i'", stats(coef) noaster ctitle("rain control, LED")  keep (`var'2res) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))
	*cap erase  eff_ledinteraction_yearbymonth_7dayDL_`i'.txt
	
	boottest  `var'1ledres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "eff_ledinteraction_yearbymonth_7dayDL_`i'", stats(coef) noaster ctitle("rain control, LED") keep (`var'1ledres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))

	boottest  `var'2ledres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "eff_ledinteraction_yearbymonth_7dayDL_`i'", stats(coef) noaster ctitle("rain control, LED")  keep (`var'2ledres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))

	boottest ledres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "eff_ledinteraction_yearbymonth_7dayDL_`i'", stats(coef) noaster ctitle("rain control, LED")  keep (ledres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))
	*cap erase  eff_ledinteraction_yearbymonth_7dayDL_`i'.txt
	
	boottest precipitationres, seed(10101) bootcluster(unit) 
	getci
	di "To here"
	local rp=r(p)
	di `rp'
	outreg2 using "eff_ledinteraction_yearbymonth_7dayDL_`i'", stats(coef) noaster ctitle("rain control, LED")  keep (precipitationres) excel nocons append addstat(pvalue, r(p), LCI,scalar(lci),UCI,scalar(uci))
	*cap erase  eff_ledinteraction_yearbymonth_7dayDL_`i'.txt
	
}
}
